<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>demo附件上传</title>
<jsp:include page="/WEB-INF/inc/systemInc.jsp"></jsp:include>
<c:if test="${fn:contains(LOGIN_USER.moduleList, 'admin/role/editPage')}">
  <script type="text/javascript">
      $.canEdit = true;
    </script>
</c:if>
<c:if test="${fn:contains(LOGIN_USER.moduleList, 'admin/role/delete')}">
  <script type="text/javascript">
      $.canDelete = true;
    </script>
</c:if>
<c:if test="${fn:contains(LOGIN_USER.moduleList, 'admin/role/grantPage')}">
  <script type="text/javascript">
      $.canGrant = true;
    </script>
</c:if>
<script type="text/javascript">
  var dataGrid;
  $(function() {
    dataGrid = $('#dataGrid')
        .datagrid(
            {
              url : '${pageContext.request.contextPath}/content/dataGrid',
              fit : true,
              rownumbers : true,
              fitColumns : true,
              border : false,
              striped : true,
              pagination : true,
              idField : 'id',
              pageSize : 10,
              pageList : [ 10, 20, 30, 40, 50 ],
              sortName : 'title',
              sortOrder : 'asc',
              checkOnSelect : false,
              selectOnCheck : false,
              nowrap : true,
              toolbar : '#toolbar',
              frozenColumns : [ [ {
                field : 'id',
                title : '编号',
                width : 150,
                checkbox : true
              }, {
                field : 'title',
                title : '文章标题',
                width : 150
              } ] ],
              columns : [ [
                  {
                    field : 'subTitle',
                    title : '子标题',
                    width : 40
                  },
                  {
                    field : 'author',
                    title : '作者',
                    width : 150,
                    hidden : true
                  },
                  {
                    field : 'source',
                    title : '来源',
                    width : 80/* ,
                    formatter : function(value, row) {
                      if (value && value == 1) {
                        return '内置角色';
                      }
                      return '普通角色';
                    } */
                  },
                  {
                    field : 'isOutLink',
                    title : '是否外部链接',
                    width : 50,
                    formatter : function(value, row) {
                      if (value=='0') {
                        return "非链接";
                      }else{
                        return "链接";
                      }
                    }
                  
                  },
                  {
                    field : 'link',
                    title : '链接',
                    width :50,
                    formatter : function(value, row) {
                      if (value) {
                        return row.link;
                      }
                      return '';
                    }
                  },
                  {
                    field : 'publishTimeStr',
                    title : '发布时间',
                    width : 80,
                    hidden : false
                  },
                  {
                    field : 'status',
                    title : '状态',
                    width : 50,
                    formatter : function(value, row) {
                      if (value== '0') {
                        return '未发布';
                      }else if(value== '1'){
                        return '发布';
                      }else{
                        return '已删除';
                      }
                     
                    }
               
                  },
                  {
                    field : 'action',
                    title : '操作',
                    width : 70,
                    formatter : function(value, row, index) {

                      var roleIsBuildin = row.roleIsBuildin;

                      var str = '';
                      if ($.canEdit) {
                        if (!roleIsBuildin || roleIsBuildin != 1) {
                          str += $
                              .formatString(
                                  '<img onclick="editFun(\'{0}\');" src="{1}" title="编辑"/>',
                                  row.id,
                                  '${pageContext.request.contextPath}/resources/css/images/extjs_icons/pencil.png');
                        }

                      }
                      str += '&nbsp;';
                      if ($.canGrant) {
                        str += $
                            .formatString(
                                '<img onclick="previewFun(\'{0}\');" src="{1}" title="预览"/>', row.id,
                                '${pageContext.request.contextPath}/resources/css/images/extjs_icons/bullet_star.png');
                      }
                      str += '&nbsp;';
                      if ($.canDelete) {
                        if (!roleIsBuildin || roleIsBuildin != 1) {
                          str += $
                              .formatString(
                                  '<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>',
                                  row.id,
                                  '${pageContext.request.contextPath}/resources/css/images/extjs_icons/delete.png');
                        }
                      }

                      return str;
                    }
                  } ] ],
              toolbar : '#toolbar',
              onContextMenu : function(e, row) {
                e.preventDefault();
                $(this).treegrid('unselectAll');
                $(this).treegrid('select', row.id);
                $('#menu').menu('show', {
                  left : e.pageX,
                  top : e.pageY
                });
              },
              onLoadSuccess : function() {
                parent.$.messager.progress('close');

//                 $(this).treegrid('tooltip');
              }
            });
  });

  function deleteFun(id) {
   
    if (id == undefined) {//点击右键菜单才会触发这个
      var rows = dataGrid.datagrid('getSelections');
      id = rows[0].id;
    } else {//点击操作里面的删除图标会触发这个
      dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
    }
    parent.$.messager.confirm('询问', '您是否要删除当前内容？', function(b) {
      if (b) {
        var currentUserId = '${LOGIN_USER.id}';/*当前登录用户的ID*/
        if (currentUserId != id) {
          parent.$.messager.progress({
            title : '提示',
            text : '数据处理中，请稍后....'
          });
          $.post('${pageContext.request.contextPath}/content/delete', {
            id : id
          }, function(result) {
            if (result.success) {
              parent.$.messager.alert('提示', result.msg, 'info');
              dataGrid.datagrid('reload');
            }
            parent.$.messager.progress('close');
          }, 'JSON');
        } else {
          parent.$.messager.show({
            title : '提示',
            msg : '不可以删除自己！'
          });
        }
      }
    });
  }

  function editFun(id) {
  
    if (id == undefined) {
      var rows = dataGrid.datagrid('getSelections');
      id = rows[0].id;
    } else {
      dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
    }

    parent.$.modalDialog({
      title : '编辑用户',
      width : 700,
      height : 640,
      href : '${pageContext.request.contextPath}/content/editPage?id=' + id,
      buttons : [ {
        text : '保存',
        handler : function() {
          parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
          var f = parent.$.modalDialog.handler.find('#form');
          f.submit();
        }
      } ]
    });
    
   
  }

  function addFun() {

    parent.$.modalDialog({
      title : '添加内容',
      width : 700,
      height : 540,
      href : '${pageContext.request.contextPath}/content/addPage',
      buttons : [ {
        text : '添加',
        handler : function() {
          parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
          var f = parent.$.modalDialog.handler.find('#form');
          f.submit();
        }
      } ]
    });
  }

  function redo() {
    var node = dataGrid.dataGrid('getSelected');
    if (node) {
      dataGrid.dataGrid('expandAll', node.id);
    } else {
      dataGrid.dataGrid('expandAll');
    }
  }

  function undo() {
    var node = dataGrid.dataGrid('getSelected');
    if (node) {
      dataGrid.dataGrid('collapseAll', node.id);
    } else {
      dataGrid.dataGrid('collapseAll');
    }
  }

  function previewFun(id) {
    window.open("${pageContext.request.contextPath}/content/contentDetail?id=" + id);
  }
</script>
</head>
<body>

  <div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false" title="" style="overflow: hidden;">
      <table id="dataGrid"></table>
    </div>
  </div>
  <div id="toolbar" style="display: none;">
    <c:if test="${fn:contains(LOGIN_USER.moduleList, 'admin/role/addPage')}">
      <a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton"
        data-options="plain:true,iconCls:'pencil_add'">添加</a>
    </c:if>
    <a onclick="redo();" href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'resultset_next'">展开</a> <a onclick="undo();"
      href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'resultset_previous'">折叠</a> <a
      onclick="treeGrid.treegrid('reload');" href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'database_refresh'">刷新</a>
  </div>

  <div id="menu" class="easyui-menu" style="width: 120px; display: none;">
    <c:if test="${fn:contains(LOGIN_USER.moduleList, '/roleController/addPage')}">
      <div onclick="addFun();" data-options="iconCls:'pencil_add'">增加</div>
    </c:if>
    <c:if test="${fn:contains(LOGIN_USER.moduleList, '/roleController/delete')}">
      <div onclick="deleteFun();" data-options="iconCls:'pencil_delete'">删除</div>
    </c:if>
    <c:if test="${fn:contains(LOGIN_USER.moduleList, '/roleController/editPage')}">
      <div onclick="editFun();" data-options="iconCls:'pencil'">编辑</div>
    </c:if>
  </div>
</body>
</html>
